package com.mano.mall.service.member.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.mano.commons.web.constants.WebConstants;
import com.mano.mall.api.member.QQAuthoriService;
import com.mano.mall.common.base.BaseApiService;
import com.mano.mall.common.base.BaseResponse;
import com.mano.mall.common.constants.Constants;
import com.mano.mall.common.token.GenerateToken;
import com.mano.mall.service.mapper.UserMapper;
import com.mano.mall.service.mapper.entity.UserDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @Author: zj
 * @Description:
 * @Date: Created in 15:52 2020/9/28
 * @Modified By:
 */
@RestController
public class QQAuthoriServiceImpl extends BaseApiService<JSONObject> implements QQAuthoriService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private GenerateToken generateToken;


    @Override
    public BaseResponse<JSONObject> findByOpenId(String qqOpenId) {
        if (StringUtils.isEmpty(qqOpenId)) {
            return setResultError("qqOpenId不能为空!");
        }

        // 1.根据openid查询用户信息
        UserDO userDO = userMapper.findByOpenId(qqOpenId);
        if(Objects.isNull(userDO)){
            return setResultError(Constants.HTTP_RES_CODE_NOTUSER_203, "根据qqOpenId没有查询到用户信息");
        }

        // 2.如果能够查询到用户信息,则直接生成对应的用户令牌
        String keyPrefix = WebConstants.MEMBER_TOKEN_KEYPREFIX + WebConstants.QQ_LOGINTYPE;
        Long userId = userDO.getUserId();
        String userToken = generateToken.createToken(keyPrefix, userId + "");
        JSONObject data = new JSONObject();
        data.put("token", userToken);

        // 这里其实没有做唯一登陆，
        return setResultSuccess(data);




    }
}
